Page({
  data: {
    records: [
      {
        id: 1,
        date: '2023-09-15',
        checkInTime: '08:25',
        checkOutTime: '17:30',
        status: '正常',
        location: '德子大学教学楼'
      },
      {
        id: 2,
        date: '2023-09-14',
        checkInTime: '08:40',
        checkOutTime: '17:35',
        status: '迟到',
        location: '德子大学教学楼'
      },
      {
        id: 3,
        date: '2023-09-13',
        checkInTime: '08:20',
        checkOutTime: '17:00',
        status: '早退',
        location: '德子大学教学楼'
      },
      {
        id: 4,
        date: '2023-09-12',
        checkInTime: '08:15',
        checkOutTime: '17:30',
        status: '正常',
        location: '德子大学教学楼'
      },
      {
        id: 5,
        date: '2023-09-11',
        checkInTime: '08:28',
        checkOutTime: '17:32',
        status: '正常',
        location: '德子大学教学楼'
      }
    ],
    dateRange: ['全部', '本周', '本月', '本学期'],
    dateRangeIndex: 0,
    statusFilters: ['全部', '正常', '迟到', '早退', '缺勤'],
    statusFilterIndex: 0,
    stats: {
      total: 22,
      normal: 18,
      late: 2,
      early: 1,
      absent: 1
    }
  },
  
  onLoad: function() {
    // 获取考勤记录
    this.fetchAttendanceRecords();
  },
  
  onShow: function() {
    // 页面显示时刷新数据
    this.fetchAttendanceRecords();
  },
  
  // 获取考勤记录
  fetchAttendanceRecords: function() {
    const userInfo = wx.getStorageSync('userInfo');
    const userId = userInfo.userId || userInfo.id;
    
    if (!userId) {
      console.error('缺少用户ID，无法获取考勤记录');
      return;
    }
    
    const app = getApp();
    wx.request({
      url: `${app.globalData.serverUrl}/api/attendance/student`, // 修改为正确的路由路径
      method: 'GET',
      data: {
        userId: userId,
        dateRange: this.data.dateRange || '',
        status: this.data.status || ''
      },
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      success: (res) => {
        if (res.statusCode === 200) {
          this.setData({
            attendanceList: res.data
          });
        } else {
          wx.showToast({
            title: '获取记录失败',
            icon: 'none'
          });
        }
      },
      fail: (err) => {
        console.error('请求考勤记录失败:', err);
        wx.showToast({
          title: '网络错误',
          icon: 'none'
        });
      }
    });
  },
  
  // 切换日期范围
  changeDateRange: function(e) {
    this.setData({
      dateRangeIndex: e.detail.value
    });
    this.fetchAttendanceRecords();
  },
  
  // 切换状态筛选
  changeStatusFilter: function(e) {
    this.setData({
      statusFilterIndex: e.detail.value
    });
    this.fetchAttendanceRecords();
  },
  
  // 查看考勤详情
  viewAttendanceDetail: function(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../kaoqindetail/kaoqindetail?id=' + id
    });
  },
  
  // 导出考勤记录
  exportAttendanceRecords: function() {
    wx.showToast({
      title: '导出功能暂未实现',
      icon: 'none'
    });
  }
});
            